package com.pine.sunshineelder.staff.web.handler;

import java.util.HashMap;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.pine.sunshineelder.common.StringHelper;
import com.pine.sunshineelder.common.dto.JSONResponse;
import com.pine.sunshineelder.common.dto.QueryResult;
import com.pine.sunshineelder.common.entity.StaffEntity;
import com.pine.sunshineelder.staff.ao.StaffAddAo;
import com.pine.sunshineelder.staff.ao.StaffModAo;
import com.pine.sunshineelder.staff.service.StaffService;
import com.pine.sunshineelder.staff.vo.StaffVo;

@RestController
@RequestMapping("/api")
public class StaffHandler implements StringHelper{
	@Autowired
	StaffService staffService;
	
	@PostMapping("/staff")
	public JSONResponse registerStaff(@Valid StaffAddAo ao)throws Exception {
		return JSONResponse.ok(staffService.addStaff(ao));
		
	}

	@DeleteMapping("/staff/{id}")
	public JSONResponse deleteStaff(@PathVariable String id)throws Exception{
		staffService.deleteStaff(id);
		return JSONResponse.ok();
	}
	
	@PutMapping("staff/{id}")
	public JSONResponse modifyStaff(@PathVariable String id,StaffModAo ao)throws Exception{
		Map<String, String>map=new HashMap<String, String>();
		map.put("no", ao.getNo());
		map.put("title", ao.getTitle());
		map.put("remark", ao.getRemark());
		map.put("introUrl", ao.getIntroUrl());
		map.put("male", ao.getMale());
		map.put("workPhone", ao.getWorkPhone());
		map.put("privatePhone", ao.getPrivatePhone());
		map.put("employedDate", ao.getEmployedDate());
		map.put("leaveDate", ao.getLeaveDate());
		map.put("email", ao.getEmail());
		map.put("wechat", ao.getWechat());
		map.put("address", ao.getAddress());
		map.put("image", ao.getImage());
		map.put("idCard", ao.getIdCard());
		map.put("birthday", ao.getBirthday());
		map.put("name", ao.getName());
		map.put("account", ao.getAccount());
		StaffEntity staffEntity=staffService.modeifyStaff(id, map);
		return JSONResponse.ok(staffEntity);
		
	}
	@GetMapping("/staff/{id}")
	public JSONResponse listOne(@PathVariable String id) throws Exception{
		StaffEntity queryById=staffService.queryById(id);
		return JSONResponse.ok(StaffVo.of(queryById));
		
	}
	@GetMapping("/staff")
	public JSONResponse listStaff(String key, String pageNo, String pageSize)throws Exception{
		Integer pageNoUse = parseInt(pageNo, null);
		Integer pageSizeUse = parseInt(pageSize,null);
		QueryResult<StaffEntity> result = staffService.queryBykey(key, pageNoUse, pageSizeUse);
		return JSONResponse.ok(result.convert(StaffVo::of));
	}
	
	
}
